﻿//Add App Page
function confirmation_access_option_click(checkbox){
    if (checkbox.checked) {
        hide('access_requirement');
        remove_css_class_name(ge('confirm_button'), 'disabled');
		ge('confirm_button_click').onclick = function(){
			$('confirm_add_form').submit();
		}
    }
    else {
        show('access_requirement');
        add_css_class_name(ge('confirm_button'), 'disabled');
		ge('confirm_button').onclick = null;
    }
}

function confirmation_validate_add(form){
    access_checkbox = ge('access_checkbox');
    if (!access_checkbox.checked) {
        window.alert('您必须勾选才可以继续。');
        return false;
    }
    return true;
}


//Edit Page
function show_idfields(showi, hidei){
    if (showi != '') {
        document.getElementById(showi).style.display = 'block';
    }
    if (hidei != '') {
        document.getElementById(hidei).style.display = 'none';
    }
}

function show_classfields(showc, hidec){
    var el = document.getElementById('dev_opt_field');
    if (!el) {
        return;
    }
    if (showc != '') {
        var showchild = getElC(showc + '_only', el);
        for (var i = 0; i < showchild.length; i++) {
            showchild[i].style.display = 'block';
        }
    }
    if (hidec != '') {
        var hidechild = getElC(hidec + '_only', el);
        for (var i = 0; i < hidechild.length; i++) {
            hidechild[i].style.display = 'none';
        }
    }
}

function checkappform(form){
    if (!form.agree_tos.checked) {
        window.alert('您必须同意校内的条款才可以继续。');
        return false;
    }
    return true;
}

function checkinstall(){
    var form = document.getElementById('applicationForm');
    var fields = document.getElementById('dev_inst_fields');
    if (form.can_added2.checked) {
        fields.style.display = 'block';
    }
    else {
        fields.style.display = 'none';
    }
}

function ajaxCheckCanvasPageUrl(canvasPageUrl){
    setTimeout(function(){
        var appId = $F("app_id");
        if (canvasPageUrl.value == '') {
            $('canvas_availability').className = '';
            return;
        }
		else if (canvasPageUrl.value.length < 7 || canvasPageUrl.value.length >20) {
			$('canvas_availability').className = 'canvas_check_url';
            return;
		}
        var url = '/AjaxCanvasPageUrlAuth.do';
        var pars = 'value=' + canvasPageUrl.value + '&appId=' + appId;
        var myAjax = new Ajax.Request(url, {
            method: 'post',
            parameters: pars,
            onComplete: checkCanvasUrl,
            onFailure: ajaxErr
        });
    }, 1000);
}

function checkCanvasUrl(response){
    var text = response.responseText;
    var resultBox = $('canvas_availability');
    if (text == "true") {
        resultBox.className = "canvas_available";
    }
    else
        if (text == "false") {
            resultBox.className = "canvas_not_available";
        }
}

function ajaxErr(r){
    window.alert(r.responseText);
}

//Developer Selector
function developer_typeahead_onsubmit(friend){
    var name = '';
    var id = '';
    if (friend) {
        name = 'dev_friends[]';
        display = friend.t;
        id = value = friend.i;
    }
    else {
        return false;
    }
    this.clear();
    if (ge('sd_' + id)) {
        return false;
    }
    var flist = ge('developers');
    var newfriend = document.createElement('div');
    var html = new Array();
    html.push('<span>', htmlspecialchars(display), ' </span>');
    html.push('<a href="#" onclick="return developer_remove_name(this)">移除</a>');
    html.push('<input type="hidden" id="sd_', htmlspecialchars(id), '" name="', name, '" value="', htmlspecialchars(display), ':', htmlspecialchars(value), '" />');
    newfriend.innerHTML = html.join('');
    flist.appendChild(newfriend);
    if ($('developers').childNodes.length > 5) {
        this.obj.disabled = true;
    }
    return false;
}

function developer_remove_name(obj){
    while (obj.parentNode && obj.tagName.toLowerCase() != 'div') {
        obj = obj.parentNode;
    }
    if (obj.tagName.toLowerCase() == 'div') {
        remove_node(obj);
    }
    $('dev_selector').getElementsByTagName('input')[0].disabled = false;
    return false;
}

//Diag
function closeApp(){
    var oP = document.getElementById('comfirm_diag');
    oP.parentNode.removeChild(oP);
}

function create_app_diag(){
	var pop_div;
	if (ge('comfirm_diag')) {
		pop_div = ge('comfirm_diag');
	}
	else {
		pop_div = document.createElement("div");
		pop_div.setAttribute("id", "comfirm_diag");
		pop_div.className = "generic_dialog pop_dialog";
		var par = ge('container');
	    par.parentNode.insertBefore(pop_div, par);
	}
    pop_div.innerHTML = '<div id="comfirm_diag" class="generic_dialog wallpro_dialog pop_dialog"><div class="generic_dialog_popup" style="top:125px;"><table class="pop_dialog_table" id="pop_dialog_table"><tr><td class="pop_topleft"></td><td class="pop_border"></td><td class="pop_topright"></td></tr><tr><td class="pop_border"></td><td id="pop_content" class="pop_content">载入中...</td><td class="pop_border"></td></tr><tr><td class="pop_bottomleft"></td><td class="pop_border"></td><td class="pop_bottomright"></td></tr></table></div></div>';
}

//Remove App
function diagRmApp(appid, appname, status){
	create_app_diag();
    var url = '/apps/editapps.do';
    var title = '删除"' + appname;
    var content = '<p>删除该应用，它将不会再出现在你的个人主页，功能菜单以及<a href="#nogo">编辑菜单</a>等页面。如果想要再次使用，你必须再次添加这个应用。<p><p><strong>是否真的要删除"' + appname + '"?</strong></p>';
    var pop_body = '<h2><span>' + title + '"?</span></h2><div class="dialog_content"><form method="post" action="http://app.xiaonei.com' + url + '"><div class="dialog_body"> ' + content + ' </div><div id="dialog_buttons" class="dialog_buttons"><input type="hidden" name="status" value="' + status + '" /><input type="hidden" name ="remove_app_id" value="' + appid + '"/><input type="submit" onfocus="this.blur()" value="提交" onclick="this.className=\'inputsubmit gray\';this.value=\'请稍候\';" name="dialog_confirm" id="dialog_confirm" class="inputsubmit" /><input type="button" onfocus="this.blur()" onclick="closeApp();" value="取消" class="inputsubmit gray"/></div></form></div>';
	ge('pop_content').innerHTML = pop_body;
	$( 'comfirm_diag' ).style.top = XN.EVENT.scrollTop() + 200 + 'px';
}

function diagEditApp (appId) {
	create_app_diag();
	var url = 'http://app.xiaonei.com/apps/editappsprivacy.do';
	var pars = 'appId=' + appId;
	var myAjax = new Ajax.Request(url, {
		method: 'get',
		parameters: pars,
		onComplete: function(transport){ge('pop_content').innerHTML = transport.responseText;}
	});
}

function diagAddApp1(apiKey,inviteId){
	create_app_diag();
	var url = 'http://app.xiaonei.com/apps/install.do';
	var pars = 'api_key=' + apiKey + '&invite_id=' + inviteId;
	var myAjax = new Ajax.Request(url, {
		method: 'get',
		parameters: pars,
		onComplete: function(transport){
			ge('pop_content').innerHTML = transport.responseText;
		}
	});
}

function diagAddApp(apiKey,inviteId,appName,isNat,ref){
	if (diagAddApp.panel) {
		var aA = diagAddApp.panel;
	} else {
		var aA = diagAddApp.panel = new XN.UI.panel();
	}
	if (isNat>10000||isNat==38) {
		var _i ='<p>提示: “'+ appName +'”这个应用<b>不是由校内开发的</b>';
	} else{
		var _i ='<p>提示: “'+ appName +'”这个应用<b>是由校内开发的</b>';
	}
	var y = '<div class="confirm_button" id="confirm_button"><a class="link_btn_style" id="confirm_button_click" href="#nogo"><div><div><div><span class="btn_text"><strong>安装' + appName + '这个应用</strong></span></div></div></div></a></div>';
	var n = '<a style="cursor: pointer" id="cancel_link">取消</a>';
	var _ft = '<div class="clearfix"><div class="float-right">' + y + '<div class="confirm_cancel">或者 ' + n + '</div></div></div>';
	aA.show();
	aA.clear();
	aA.setHeader('将要安装“' + appName + '”吗？');
	aA.setBody('<form method="post" action="/apps/install.do" id="confirm_add_form">' +
		'<input type="hidden" name="api_key" value="' + apiKey + '"/>' +
		'<input type="hidden" name="invite_id" value="' + inviteId + '"/>' +
		'<input type="hidden" name="ref" value="' + ref + '"/>' +
		'<input type="hidden" name="origin" value="901"/>' +
		'<p>安装后你将允许这个应用: </p>' +
		'<ul class="confirm_options">' +
		'<li><input type="checkbox" id="access_checkbox" name="access_checkbox" checked="checked" class="inputcheckbox" onclick="return confirmation_access_option_click(this);" /><label for="access_checkbox">知道我是谁并获得我的个人信息</label></li>' +
		'<li id="access_requirement" style="display:none;"><div id="access_requirement_message" style="width: 214px;">如果你不同意获取你的信息，那么你不能添加这个应用 。</div></li>' +
		'<li><input type="checkbox" id="allow_profile" name="allow_profile" checked="checked" class="inputcheckbox" value="32"/><label for="allow_profile">在个人主页应用栏上放置这个应用</label></li>' +
		// '<li><input type="checkbox" id="allow_nav_link" name="allow_nav_link" checked="checked" class="inputcheckbox" value="1"/><label for="allow_nav_link">在首页应用程序菜单栏中显示</label></li>' +
		'<li><input type="checkbox" id="allow_feeds" name="allow_feeds" checked="checked" class="inputcheckbox" value="6"/><label for="allow_feeds">发布到我的个人新鲜事和好友新鲜事</label></li>' +
		'<li><input type="checkbox" id="allow_email" name="allow_email" checked="checked" class="inputcheckbox" value="16"/><label for="allow_email">允许这个应用向我发送提醒邮件</label></li>' +
		'</ul>' + _i +
		'</form>');
	aA.setFooter(_ft);
	$('confirm_button').onclick = function(){
		$('confirm_add_form').submit();
	}
	$('cancel_link').onclick = function(){
		aA.hide();
	}
}


function diagAddApp2(app){
	if (diagAddApp.panel) {
		var aA = diagAddApp.panel;
	} else {
		var aA = diagAddApp.panel = new XN.UI.panel();
	}
	if (app.appId>10000||app.appId==38) {
		var _i ='<p>提示: 这个应用<b>不是由校内开发的</b>';
	} else{
		var _i ='<p>提示: 这个应用<b>是由校内开发的</b>';
	}
	var y = '<div class="confirm_button" id="confirm_button"><a class="link_btn_style" id="confirm_button_click" href="#nogo"><div><div><div><span class="btn_text"><strong>安装这个应用</strong></span></div></div></div></a></div>';
	var n = '<a style="cursor: pointer" id="cancel_link">取消</a>';
	var _ft = '<div class="clearfix"><div class="float-right" style="width:200px">' + y + '<div class="confirm_cancel">或者 ' + n + '</div></div></div>';
	aA.show();
	aA.clear();
	aA.setWidth(600);
	aA.setHeader('安装“' + app.appName + '”');
	aA.setBody('<div id="confirm_add_intro">' +
		'<div class="figure"><img src="' + app.logo + '" alt="' + app.appName + '" /></div>' +
		'<div class="content"><h3>' + app.appName + '</h3>' +
		'<ul class="star-rating" id="StarRating"><li style="width: ' + app.rating * 20 + '%;" class="current-rating"/></ul><div style="margin-left: 8px;" id="scorethisDiv" class="scorethisDiv"><span id="scorethis" class="scorethis"><strong>' + parseInt(app.rating) + '</strong>.' + parseInt((app.rating - parseInt(app.rating)) * 10) + '</span></div>' + 
		'<ul class="info"><li>分类: ' + app.cate + '</li>' +
		'<li>安装: ' + app.totalUser + '(' + app.activeUser + '日活跃)</li></ul></div>' +
		'<div class="intro">' + app.appShortDesc + '</div>' +
		'</div>' +
		'<form method="post" action="/apps/enterApp.do" id="confirm_add_form">' +
		'<input type="hidden" name="origin" value="' + app.origin + '"/>' +
		'<input type="hidden" name="id" value="' + app.appId + '"/>' +
		'<input type="hidden" name="invite_id" value="' + (app.inviteId? app.inviteId : '') + '"/>' +
		'<input type="hidden" name="ref" value="' + (app.ref? app.ref : '') + '"/>' +
		'<input type="hidden" name="access_checkbox" />' +
		'<input type="hidden" name="allow_profile"  value="on"/>' +
		'<input type="hidden" name="allow_feeds" value="6"/>' +
		'<input type="hidden" name="origin" value="901"/>' +
		'<p>安装后你将允许这个应用: </p>' +
		'<ul class="confirm_options">' +
		'<li><input type="checkbox" id="access_checkbox" name="access_checkbox" checked="checked" class="inputcheckbox" onclick="return confirmation_access_option_click(this);" /><label for="access_checkbox">知道我是谁并获得我的个人信息</label></li>' +
		'<li id="access_requirement" style="display:none;"><div id="access_requirement_message">如果你不同意获取你的信息，那么你不能添加这个应用 。</div></li>' +
		'<li><input type="checkbox" id="allow_profile" name="allow_profile" checked="checked" class="inputcheckbox" value="32"/><label for="allow_profile">在个人主页应用栏上放置这个应用</label></li>' +
		// '<li><input type="checkbox" id="allow_nav_link" name="allow_nav_link" checked="checked" class="inputcheckbox" value="1"/><label for="allow_nav_link">在首页应用程序菜单栏中显示</label></li>' +
		'<li><input type="checkbox" id="allow_feeds" name="allow_feeds" checked="checked" class="inputcheckbox" value="6"/><label for="allow_feeds">发布到我的个人新鲜事和好友新鲜事</label></li>' +
		'<li><input type="checkbox" id="allow_email" name="allow_email" checked="checked" class="inputcheckbox" value="16"/><label for="allow_email">允许这个应用向我发送提醒邮件</label></li>' +
		'</ul>' + _i +
		'</form>');
	
	aA.setFooter(_ft);
	
	$('confirm_button').onclick = function(){
		$('confirm_add_form').submit();
	}
	$('cancel_link').onclick = function(){
		aA.hide();
	}
}
/**
 * app 新版产品目录中，点击一个app时，判断用户是否安装了此app的JS函数（added by lifs）
 * @param app
 * @param sideNavUrl
 * @return
 */
function ajaxCheckAppInstall(app, sideNavUrl){
	var url = '/AjaxIsAlreadyAddApp.do';
	var pars = 'id=' + app.appId;
	var myAjax = new Ajax.Request(url, {
		method: 'post',
		parameters: pars,
		onComplete: function (response) {
			checkAppInstalled(response, app, sideNavUrl);
		}
	});
}
/**
 * 判断用户是否安装了此app的JS函数（added by lifs）
 * @param response
 * @param app
 * @param sideNavUrl
 * @return
 */
function checkAppInstalled(response, app, sideNavUrl){
    var text = response.responseText;
	if(sideNavUrl.length < 1){
		sideNavUrl = 'http://app.xiaonei.com/apps/application.do?id=' + app.appId;
	}
    if (text == "true") {
        self.location = sideNavUrl;
    } else if (text == "false") {
		diagAddAppProductList(app);
	}
}
/**
 * 弹出安装选项层，应用于app 新版产品目录中，（added by lifs）
 * @param app
 * @return
 */
function diagAddAppProductList(app){
	if (diagAddApp.panel) {
		var aA = diagAddApp.panel;
	} else {
		var aA = diagAddApp.panel = new XN.UI.panel();
	}
	if (app.appId>10000||app.appId==38) {
		var _i ='<p>提示: 这个应用<b>不是由校内开发的</b>';
	} else{
		var _i ='<p>提示: 这个应用<b>是由校内开发的</b>';
	}
	var y = '<div class="confirm_button" id="confirm_button"><a class="link_btn_style" id="confirm_button_click" href="#nogo"><div><div><div><span class="btn_text"><strong>安装这个应用</strong></span></div></div></div></a></div>';
	var n = '<a style="cursor: pointer" id="cancel_link">取消</a>';
	var _ft = '<div class="clearfix"><div class="float-right" style="width:200px;">' + y + '<div class="confirm_cancel">或者 ' + n + '</div></div></div>';
	aA.show();
	aA.clear();
	aA.setWidth(600);
	aA.setHeader('安装“' + app.appName + '”');
	aA.setBody('<div id="confirm_add_intro">' +
		'<div class="figure"><img src="' + app.logo + '" alt="' + app.appName + '" /></div>' +
		'<div class="content"><h3>' + app.appName + '</h3>' +
		'<ul class="star-rating" id="StarRating"><li style="width: ' + app.rating * 20 + '%;" class="current-rating"/></ul><div style="margin-left: 8px;" id="scorethisDiv" class="scorethisDiv"><span id="scorethis" class="scorethis"><strong>' + parseInt(app.rating) + '</strong>.' + parseInt((app.rating - parseInt(app.rating)) * 10) + '</span></div>' + 
		'<ul class="info"><li>分类: ' + app.cate + '</li>' +
		'<li>安装: ' + app.totalUser + '(' + app.activeUser + '日活跃)</li></ul></div>' +
		'<div class="intro">' + app.appShortDesc + '</div>' +
		'</div>' +
		'<form method="post" action="/apps/enterApp.do" id="confirm_add_form">' +
		'<input type="hidden" name="origin" value="' + app.origin + '"/>' +
		'<input type="hidden" name="id" value="' + app.appId + '"/>' +
		'<input type="hidden" name="invite_id" value="' + (app.inviteId? app.inviteId : '') + '"/>' +
		'<input type="hidden" name="ref" value="' + (app.ref? app.ref : '') + '"/>' +
		'<input type="hidden" name="access_checkbox" />' +
		'<input type="hidden" name="allow_profile"  value="32"/>' +
		'<input type="hidden" name="allow_feeds" value="6"/>' +
		'<p>安装后你将允许这个应用: </p>' +
		'<ul class="confirm_options">' +
		'<li><input type="checkbox" id="access_checkbox" name="access_checkbox" checked="checked" class="inputcheckbox" onclick="return confirmation_access_option_click(this);" /><label for="access_checkbox">知道我是谁并获得我的个人信息</label></li>' +
		'<li id="access_requirement" style="display:none;"><div id="access_requirement_message">如果你不同意获取你的信息，那么你不能添加这个应用 。</div></li>' +
		'<li><input type="checkbox" id="allow_profile" name="allow_profile" checked="checked" class="inputcheckbox" value="32"/><label for="allow_profile">在个人主页应用栏上放置这个应用插件</label></li>' +
		// '<li><input type="checkbox" id="allow_nav_link" name="allow_nav_link" checked="checked" class="inputcheckbox" value="1"/><label for="allow_nav_link">在首页应用程序菜单栏中显示</label></li>' +
		'<li><input type="checkbox" id="allow_feeds" name="allow_feeds" checked="checked" class="inputcheckbox" value="6"/><label for="allow_feeds">发布到我的个人新鲜事和好友新鲜事</label></li>' +
		'<li><input type="checkbox" id="allow_email" name="allow_email" checked="checked" class="inputcheckbox" value="16"/><label for="allow_email">允许这个应用向我发送提醒邮件</label></li>' +
		'</ul>' + _i +
		'</form>');
	
	aA.setFooter(_ft);
	
	$('confirm_button').onclick = function(){
		$('confirm_add_form').submit();
	}
	$('cancel_link').onclick = function(){
		aA.hide();
	}
}

function ajaxAddAppComment(pars){
	var postUrl = "/AjaxAppComment.do";
	var data = 'isAlreadyAddApp=' + pars.isAlreadyAddApp;
	new XN.NET.xmlhttp({
		url:postUrl,
		data:data,
		method:'post',
		onSuccess:function(r) {
			if( (r!=null) ||(r.trim().length()!=0) ){
				var comment = eval('(' + r.responseText + ')');
				loadAppComment(comment);
			}
		},
		onError:ajaxErr
	});		
}

function loadAppComment(list){
	var comments = list.comments;
	var comment = comments[1];
	var dom_comment_list = document.getElementById("commentList");
	var cmt_div = document.createElement("div");
	cmt_div.innerHTML='<div class="cmt-cell clearfix" ><div class="app-user-face float-left"><a href="http://xiaonei.com/profile.do?id=' + pars.userId +'"><img src="' + comment.fullHeadUrl + '" width="50px"/></a></div><dl class="app-user-inf float-left"><dt class="clearfix"><a href="http://xiaonei.com/getuser.do?id='+ pars.userId +'">' + pars.userName + '</a>() <span class="time">' + comment.commentTime + '</span></dt><dd ><div class="domarks clearfix"><ul class="star-rating" title="' + comment.appRating + '分"><li class="current-rating" style="width:100.0%;"></li></ul></div></dd></dl></div>';
	dom_comment_list.insertBefore(cmt_div,dom_comment_list.firstChild);
}

function ajaxGetMoreList(selected,developerType,category,origin,op){
	var newTab = document.getElementById("changeTab");
	newTab.innerHTML='<div style="text-align:center;margin-top:50px;"><h4 class="h4_fix">加载中，请稍候……</h4><img src="http://xnimg.cn/imgpro/indicator/blue_large.gif" style="vertical-align: middle;horizon-align:middle;"/></div>';
	var url = '/apps/apps.do';
	var pars = 'selected='+selected+'&type='+developerType+'&category='+category+'&origin='+origin+'&op='+op;
	var myAjax = new Ajax.Request(url, {
		method: 'post',
		parameters: pars,
		onSuccess:changeCate,
		onFailure: ajaxErr
	});
	return false;
}

function ajaxChangeAppTab(selected,developerType,category,op,keyword){
	var newTab = document.getElementById("changeTab");
	newTab.innerHTML='<div style="text-align:center;margin-top:50px;"><h4 class="h4_fix">加载中，请稍候……</h4><img src="http://xnimg.cn/imgpro/indicator/blue_large.gif" style="vertical-align: middle;horizon-align:middle;"/></div>';
	var url = '/apps/apps.do';
	var pars = 'selected='+selected+'&type='+developerType+'&category='+category+'&op='+op+'&keyword='+keyword;
	var myAjax = new Ajax.Request(url, {
		method: 'post',
		parameters: pars,
		onSuccess: changeCate,
		onFailure: ajaxErr
	});
	return false;
}

function ajaxSearchApp(){
	var newTab = document.getElementById("changeTab");
	newTab.innerHTML='<div style="text-align:center;margin-top:50px;"><h4 class="h4_fix">加载中，请稍候……</h4><img src="http://xnimg.cn/imgpro/indicator/blue_large.gif" style="vertical-align: middle;horizon-align:middle;"/></div>';
	var url = '/apps/apps.do';
	var keyword = $('query_name').value;
	var pars = 'origin=603'+'&op=search'+'&keyword='+keyword;
	var myAjax = new Ajax.Request(url, {
		method: 'post',
		parameters: pars,
		onSuccess: changeCate,
		onFailure: ajaxErr
	});
	return false;
}

function changeCate(response){
	var newTab = document.getElementById("changeTab");
	newTab.innerHTML=response.responseText;
	var scripts = newTab.getElementsByTagName("script");
	for (var i = 0; i < scripts.length; i++) {
		eval(scripts[i].innerHTML);
	}
}